package com.ruoyi.web.controller.order;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.order.domain.HotProduct;
import com.ruoyi.order.domain.Vo.CategoryVo;
import com.ruoyi.order.domain.Vo.ProductVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.Category;
import com.ruoyi.order.service.ICategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 类别Controller
 * 
 * @author ruoyi
 * @date 2022-02-04
 */
@Controller
@RequestMapping("/order/category")
public class CategoryController extends BaseController
{
    private String prefix = "order/category";

    @Autowired
    private ICategoryService categoryService;

    @RequiresPermissions("order:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询类别树列表
     */
    @RequiresPermissions("order:category:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Category> list(Category category)
    {
        List<Category> list = categoryService.selectCategoryList(category);
        return list;
    }

    /**
     * 导出类别列表
     */
    @RequiresPermissions("order:category:export")
    @Log(title = "类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Category category)
    {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "类别数据");
    }

    /**
     * 新增类别
     */
    @GetMapping(value = { "/add/{categoryID}", "/add/" })
    public String add(@PathVariable(value = "categoryID", required = false) Integer categoryID, ModelMap mmap)
    {
        if (StringUtils.isNotNull(categoryID))
        {
            mmap.put("category", categoryService.selectCategoryByCategoryID(categoryID));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存类别
     */
    @RequiresPermissions("order:category:add")
    @Log(title = "类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Category category)
    {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改类别
     */
    @RequiresPermissions("order:category:edit")
    @GetMapping("/edit/{categoryID}")
    public String edit(@PathVariable("categoryID") Integer categoryID, ModelMap mmap)
    {
        Category category = categoryService.selectCategoryByCategoryID(categoryID);
        mmap.put("category", category);
        return prefix + "/edit";
    }

    /**
     * 修改保存类别
     */
    @RequiresPermissions("order:category:edit")
    @Log(title = "类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Category category)
    {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除
     */
    @RequiresPermissions("order:category:remove")
    @Log(title = "类别", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{categoryID}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("categoryID") Integer categoryID)
    {
        return toAjax(categoryService.deleteCategoryByCategoryID(categoryID));
    }

    /**
     * 选择类别树
     */
    @GetMapping(value = { "/selectCategoryTree/{categoryID}", "/selectCategoryTree/" })
    public String selectCategoryTree(@PathVariable(value = "categoryID", required = false) Integer categoryID, ModelMap mmap)
    {
        if (StringUtils.isNotNull(categoryID))
        {
            mmap.put("category", categoryService.selectCategoryByCategoryID(categoryID));
        }
        return prefix + "/tree";
    }

    /**
     * 加载类别树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = categoryService.selectCategoryTree();
        return ztrees;
    }

    /**
     * 查询热销产品列表信息
     */
    @RequestMapping("/getCategoryPage")
    @ResponseBody
    public TableDataInfo getCategoryPage(String categoryName )
    {
        startPage();
        List<CategoryVo> list = categoryService.getCategoryPage(categoryName);
        return getDataTable(list);
    }


    /**
     * 查询热销产品列表信息
     */
    @RequestMapping("/getCategoryPageList")
    @ResponseBody
    public TableDataInfo getCategoryPageList()
    {
        startPage();
        List<CategoryVo> list = categoryService.getCategoryPageList();
        return getDataTable(list);
    }


    /**
     * 查询热销产品列表信息
     */
    @PostMapping("/getCategoryParentPage")
    @ResponseBody
    public TableDataInfo getCategoryParentPage()
    {
        startPage();
        List<CategoryVo> list = categoryService.getCategoryParentPage();
        return getDataTable(list);
    }
    /**
     * 根据商品目录id获取产品
     */
    @RequestMapping("/getCategoryProduct")
    public TableDataInfo getCategoryProduct(Integer categoryID)
    {
        startPage();
        List<ProductVo> categoryProduct = categoryService.getCategoryProduct(categoryID);
        return getDataTable(categoryProduct);
    }
}
